package com.taru.wxhsh.service.impl;

import com.taru.wxhsh.mapper.DetailMapper;
import com.taru.wxhsh.mapper.OrderMapper;
import com.taru.wxhsh.pojo.Detail;
import com.taru.wxhsh.pojo.Order;
import com.taru.wxhsh.service.OrderService;
import com.taru.wxhsh.util.UidGenerators;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by HuKang on 2018/5/29.
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    OrderMapper orderMapper;

    @Autowired
    DetailMapper detailMapper;


    @Override
    public List<Order> selectListWithUser() {
        return orderMapper.selectListWithUser();
    }

    @Override
    public List<Order> selcetById(String uid) {
        return orderMapper.selcetById(uid);
    }

    @Override
    public List<Detail> selectByOrderId(String orderid) {
        return detailMapper.selectByOrderId(orderid);
    }

    @Override
    public Order selectBy_OrderId(String orderid) {
        return orderMapper.selectBy_OrderId(orderid);
    }

    @Override
    public void send(String orderid) {
        orderMapper.send(orderid);
    }

    @Override
    public void invalid(String orderid) {
        orderMapper.invalid(orderid);
    }

    /**
     * 添加订单
     * @param order
     */
    @Override
    @Transactional(readOnly = false)
    public String addOrder(Order order) {
        String orderId =UidGenerators.getDateId();
        order.setOrderId(orderId);
        orderMapper.insert(order);
        //更新订单编号
        if(order.getUser()!=null){
            detailMapper.updateOrderByUserId(order.getOrderId(),order.getUser().getUserId());;
        }
        return orderId;

    }
}
